﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using SRW.BLL;
using SRW;

public partial class ks : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //初始化时钟
            TimeSs.Text = "60";
            TimeMm.Text = "1";
            LinkButton1.Enabled = false;
            LinkButton5.Visible = false;
            int pkID = int.Parse(Request.QueryString["ID"].ToString());
            string sinoname = SQL.GetData("select distinct sinoname from sinopro where pkID=" + pkID + "", "sinoname");
            LabType.Text = "现在系统已经为你抽取一份" + sinoname + "专项的试题！";
            Label3.Visible = false;
            Session["selMark"] = 0;
            Session["PanDMark"] = 0;
            Session["IsEqual"] = -1;//可以设为任何小于0的数，这里设为-1
        }
        if (TimeMm.Text == "0" && TimeSs.Text == "0")
        {
            LinkButton3.Visible = false;
            LinkButton4.Visible = false;
            LinkButton5.Visible = true;
            Label3.Visible = true;
            RBL1.Enabled = false;
        }
        if (Session["ProSeList"] == null)
        {
            timu.Text = "选择题";
            shuoming.Text = "共40题选择题，每题2分！(提醒：在做题过程中请勿刷新页面！)";

            List<Wtproblem> mList = GetProList("选择题");
            Session["ProSeList"] = mList;
            Session["SeIndex"] = 1;
            if (Session["SeIndex"] != null && mList != null)
            {
                LbTitle.Text = "第" + ((int)Session["SeIndex"]) + "题：" + mList[(int)Session["SeIndex"] - 1].wtproname;

                RBL1.DataTextField = "wtanname";
                RBL1.DataValueField = "wtanname";
                RBL1.DataSource = SQL.GetDataTable("wtanswer", "select wtanname from wtanswer where problemID=" + mList[(int)Session["SeIndex"] - 1].pkID + "");
                RBL1.DataBind();
            }
        }
    }
    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (TimeMm.Text == "0" && TimeSs.Text == "0")
        {
            TimeMm.Text = "0";
            TimeSs.Text = "0";
            TimeMm.UpdateAfterCallBack = true;
            TimeSs.UpdateAfterCallBack = true;
            return;
        }
        TimeSs.Text = Convert.ToString(int.Parse(TimeSs.Text) - 1);
        if (int.Parse(TimeSs.Text) < 0)
        {
            TimeMm.Text = Convert.ToString(int.Parse(TimeMm.Text) - 1);
            TimeSs.Text = "59";
            TimeMm.UpdateAfterCallBack = true;
        }
        TimeSs.UpdateAfterCallBack = true;
    }
    public List<Wtproblem> GetProList(string ProType)
    {
        int pkID = int.Parse(Request.QueryString["ID"].ToString());
        string sinoname = SQL.GetData("select distinct sinoname from sinopro where pkID=" + pkID + "", "sinoname");
        List<string> mList = bWtpaper.GetPaper("select distinct papername from wtpaper where sinoname='" + sinoname + "'", "papername");
        Random rnd = new Random();

        string papername = "";
        if (Session["indexPage"] == null)
        {
            Session["indexPage"] = rnd.Next(mList.Count);
            papername = mList[(int)Session["indexPage"]];
        }
        else
        {
            papername = mList[(int)Session["indexPage"]];
        }
        Session["paperName"] = papername;
        List<string> mProIdList = bWtpaper.GetPaper("select distinct problemID from wtpaper where papername='" + papername + "'", "problemID");

        List<Wtproblem> results = new List<Wtproblem>();

        for (int i = 0; i<mProIdList.Count; i++)
        {
            Wtproblem mWtproblem = bWtproblem.select("select * from wtproblem where pkID=" + int.Parse(mProIdList[i].ToString()) + "");
            if (mWtproblem.wtprotype == ProType)
            {
                results.Add(mWtproblem);
            }
        }
        return results;
    }
    protected void RBL1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (TimeMm.Text == "0" && TimeSs.Text == "0")
        {
            return;
        }
        List<Wtproblem> mList = null;
        if (Session["ProSeList"] != null && Session["SeIndex"] != null)
        {
            mList = (List<Wtproblem>)Session["ProSeList"];
        }
        else
        {
            return;
        }
        string rightname = SQL.GetData("select rightname from rightan where problemID=" + mList[(int)Session["SeIndex"] - 1].pkID + "", "rightname");

        Labmy.Visible = true;

        Labmy.Text = "你的答案是：" + RBL1.SelectedItem.Text.ToString();

        if (!RBL1.SelectedItem.Text.ToString().Equals(rightname))
        {
            //错误，则记录分数，选择题最后得分以80-session作为最后分数，判断题以20-session作为最后分数
            //未保证练习的有效性，分数以第一次选择的答案作为标准
            if (mList[(int)Session["SeIndex"] - 1].wtprotype == "选择题" && ((int)Session["IsEqual"] != ((int)Session["SeIndex"] - 1)))
            {
                Session["selMark"] = (int)Session["selMark"] + 2;
            }
            else if (mList[(int)Session["SeIndex"] - 1].wtprotype == "判断题" && ((int)Session["IsEqual"] != ((int)Session["SeIndex"] - 1)))
            {
                Session["PanDMark"] = (int)Session["PanDMark"] + 1;
            }
        }
        if ((int)Session["SeIndex"] == 20 && mList[(int)Session["SeIndex"] - 1].wtprotype == "判断题")
        {
            Finishtimu.Visible = true;
            Finishtimu.Text = "你已经完成该份练习，现在可以提交！";

        }


        Session["IsEqual"] = (int)Session["SeIndex"] - 1;



    }
    protected void LinkButton4_Click(object sender, EventArgs e)
    {
        //下一题
        if (TimeMm.Text == "0" && TimeSs.Text == "0")
        {
            return;
        }

        if (((int)Session["IsEqual"] != ((int)Session["SeIndex"] - 1)) && ((List<Wtproblem>)Session["ProSeList"])[(int)Session["SeIndex"] - 1].wtprotype == "选择题")
        {
            Session["selMark"] = (int)Session["selMark"] + 2;
        }
        else if (((int)Session["IsEqual"] != ((int)Session["SeIndex"] - 1)) && ((List<Wtproblem>)Session["ProSeList"])[(int)Session["SeIndex"] - 1].wtprotype == "判断题")
        {
            Session["PanDMark"] = (int)Session["PanDMark"] + 1;
        }
        Labmy.Visible = false;
       
        Finishtimu.Visible = false;

        List<Wtproblem> mList = null;
        if (Session["SeIndex"] != null)
        {

            Session["SeIndex"] = (int)Session["SeIndex"] + 1;

            if ((int)Session["SeIndex"] > 40 && ((List<Wtproblem>)Session["ProSeList"])[(int)Session["SeIndex"] - 2].wtprotype == "选择题")
            {
                timu.Text = "判断题";
                shuoming.Text = "共20题判断题，每题1分！(提醒：在做题过程中请勿刷新页面！)";

                mList = GetProList("判断题");
                Session["ProSeList"] = mList;
                Session["SeIndex"] = 1;
                LbTitle.Text = "第" + ((int)Session["SeIndex"]) + "题：" + mList[(int)Session["SeIndex"] - 1].wtproname;

                RBL1.DataTextField = "wtanname";
                RBL1.DataValueField = "wtanname";
                RBL1.DataSource = SQL.GetDataTable("wtanswer", "select wtanname from wtanswer where problemID=" + mList[(int)Session["SeIndex"] - 1].pkID + "");
                RBL1.DataBind();

                return;
            }
            if ((int)Session["SeIndex"] == 21 && ((List<Wtproblem>)Session["ProSeList"])[(int)Session["SeIndex"] - 2].wtprotype == "判断题")
            {
                Finishtimu.Visible = true;
                Finishtimu.Text = "你已经完成该份练习，现在可以提交！";
                LinkButton4.Enabled = false;
                LinkButton5.Visible = true;

                return;
            }
        }
        else
        {
            return;
        }
        mList = null;
        if (Session["ProSeList"] != null)
        {
            mList = (List<Wtproblem>)Session["ProSeList"];
        }
        LbTitle.Text = "第" + ((int)Session["SeIndex"]) + "题：" + mList[(int)Session["SeIndex"] - 1].wtproname;

        RBL1.DataTextField = "wtanname";
        RBL1.DataValueField = "wtanname";
        RBL1.DataSource = SQL.GetDataTable("wtanswer", "select wtanname from wtanswer where problemID=" + mList[(int)Session["SeIndex"] - 1].pkID + "");
        RBL1.DataBind();
    }
    protected void LinkButton5_Click(object sender, EventArgs e)
    {
        //提交练习，说明，这里的学号，在这个系统中就是登陆的用户名
        if (Session["paperName"] != null && Session["PanDMark"] != null && Session["selMark"] != null && Session["username"] != null)
        {
            Wtmark mWtmark = new Wtmark();
            int pkID = int.Parse(Request.QueryString["ID"].ToString());
            mWtmark.markm = SQL.GetData("select distinct sinoname from sinopro where pkID=" + pkID + "", "sinoname");
            mWtmark.markrigfa = 20 - (int)Session["PanDMark"];
            mWtmark.marksingse = 80 - (int)Session["selMark"];
            mWtmark.papertest = Session["paperName"].ToString();
            mWtmark.stunumber = Convert.ToString(Session["username"]);
            mWtmark.totalmark = mWtmark.markrigfa + mWtmark.marksingse;
            if (bWtmark.Insert(mWtmark, false))
            {
                MagicAjax.AjaxCallHelper.WriteAlert("已经提交！");
                LinkButton3.Enabled = false;
                //LinkButton5.Enabled = false;
                RBL1.Enabled = false;
                CookieOper.CleanSession("ProSeList");
                CookieOper.CleanSession("SeIndex");
                CookieOper.CleanSession("selMark");
                CookieOper.CleanSession("PanDMark");
                CookieOper.CleanSession("IsEqual");
                CookieOper.CleanSession("paperName");
                CookieOper.CleanSession("indexPage");
            }
        }
        else
        {
            CookieOper.CleanSession("username");
            CookieOper.CleanSession("usertype");
            CookieOper.CleanSession("stuproType");
            CookieOper.CleanSession("ProSeList");
            CookieOper.CleanSession("SeIndex");
            CookieOper.CleanSession("selMark");
            CookieOper.CleanSession("PanDMark");
            CookieOper.CleanSession("IsEqual");
            CookieOper.CleanSession("indexPage");
            JScript.AlertAndRedirect("系统出现错误，请重新登录！", "leks.aspx");
        }
    }
}

